package com.wzj.yygh.hosp.controller.admin;

import com.wzj.yygh.common.exception.YyghException;
import com.wzj.yygh.common.result.R;
import com.wzj.yygh.common.result.ResultCode;
import com.wzj.yygh.hosp.service.DoctorService;
import com.wzj.yygh.hosp.service.IDoctorSchedulingService;
import com.wzj.yygh.model.hosp.Doctor;
import com.wzj.yygh.model.hosp.DoctorScheduling;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "医生排班管理")
@RequestMapping("/admin/hosp/doctorScheduling")
@Transactional
public class DoctorSchedulingController {

    @Autowired
    private IDoctorSchedulingService iDoctorSchedulingService;

    @Autowired
    private DoctorService iDoctorService;

    @PostMapping(value = "/addNumber")
    @ApiOperation(value = "医生放号")
    public R addNumber(@RequestParam String doctorId, @RequestParam String date, @RequestParam int step, @RequestParam int number){
        Doctor doctor = iDoctorService.getById(doctorId);
        if(doctor == null) {
            return R.error().message("医生不存在");
        }
        if(number < 1) {
            return R.error().message("放号数必须大于0");
        }
        for(int i = 1 ; i <= number; i ++) {
            DoctorScheduling ds = new DoctorScheduling();
            ds.setDoctorId(doctor.getId().toString());
            ds.setDoctorName(doctor.getDoctorName());
            ds.setNumber(i + "");
            ds.setStep(step + "");
            ds.setDate(date);
            ds.setOrderFlag(0);
            iDoctorSchedulingService.saveOrUpdate(ds);
        }
        return R.ok().message("放号成功");
    }

    @GetMapping("findAll")
    @ApiOperation(value = "查询所有医生排班")
    public R getAll(){
        List<DoctorScheduling> doctorSchedulings = iDoctorSchedulingService.list();
        return R.ok().data("list",doctorSchedulings);
    }

//    @RequestMapping(value = "/getByPage", method = RequestMethod.GET)
//    @ApiOperation(value = "查询医生排班")
//    public R getByPage(@ModelAttribute DoctorScheduling scheduling){
//        QueryWrapper<DoctorScheduling> qw = new QueryWrapper<>();
//        if(!StringUtils.isEmpty(scheduling.getDoctorId())) {
//            qw.eq("doctor_id",scheduling.getDoctorId());
//        }
//        if(!StringUtils.isEmpty(scheduling.getDate())) {
//            qw.eq("date",scheduling.getDate());
//        }
//        if(scheduling.getStep().equals("0")) {
//            qw.eq("step","0");
//        } else if(scheduling.getStep().equals("1")) {
//            qw.eq("step","1");
//        }
//        Page<Hospital> pageModel = iDoctorSchedulingService.Page(page, limit, hosname);
//        return R.ok().data("pageModel",pageModel);
//
//        return new ResultUtil<IPage<DoctorScheduling>>().setData(iDoctorSchedulingService.page(PageUtil.initMpPage(page),qw));
//    }

    @ApiOperation("根据ID获取科室信息")
    @GetMapping("getDoctorScheduling/{id}")
    public R getDoctorSet(@PathVariable Long id) {

        DoctorScheduling doctorScheduling = iDoctorSchedulingService.getById(id);
        return R.ok().data("item",doctorScheduling);
    }

    @ApiOperation("修改科室信息")
    @PutMapping("updateDoctorScheduling")
    public R updateDoctorSet(@RequestBody DoctorScheduling doctorScheduling) {
        boolean flag = iDoctorSchedulingService.updateById(doctorScheduling);
        if(flag) {
            return R.ok().message("修改成功");
        } else {
            return R.error().message("修改失败");
        }
    }

    @ApiOperation(value = "逻辑删除医生排班信息")
    @DeleteMapping("{id}")
    public R removeDoctor(
            @ApiParam(value = "id",required = true)
            @PathVariable Long id) {
        try {
            boolean flag = iDoctorSchedulingService.removeById(id);
            if(flag) {
                return R.ok().message("删除成功");
            } else {
                return R.error().message("删除失败").code(ResultCode.ERROR_DELETE);
            }
        } catch (Exception e) {
            throw new YyghException(ResultCode.ERROR,"删除科室信息错误",e);
        }
    }

    @ApiOperation("批量删除医生排班信息")
    @DeleteMapping("batchRemove")
    public R batchRemoveDoctor(@RequestBody List<Long> idList) {
        boolean result = iDoctorSchedulingService.removeByIds(idList);
        if (result){
            return R.ok().message("删除成功");
        }else {
            return R.error().message("删除失败");
        }
    }
}